﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmSupplierEdit : XtraForm
    {
        #region Properties
        private SupplierManager supplierManager;
        public SupplierInfo supplier { get; set; }
       
        #endregion

        #region Init Form
        public frmSupplierEdit()
        {
            InitializeComponent();

            // Initialize all properties of this form
            this.supplierManager = new SupplierManager();
            this.supplier = new SupplierInfo();
            
        }

        
        private void frmSupplierEdit_Load_1(object sender, EventArgs e)
        {
            this.txtName.Text = this.supplier.Name;
            this.txtCode.Text = this.supplier.Code;
            this.txtLiabilities.Text = this.supplier.Liabilities.ToString();
            this.txtAddress.Text = this.supplier.Address;
            this.txtMobile.Text = this.supplier.Mobile;
            this.txtPhone.Text = this.supplier.Phone;
            this.txtEmail.Text = this.supplier.Email;
            this.txtFax.Text = this.supplier.Fax;
            this.txtTaxCode.Text = this.supplier.TaxCode;
            this.txtWebsite.Text = this.supplier.Website;
            this.txtDescription.Text = this.supplier.Description;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtContactor.Text = this.supplier.Contactor;
            this.txtContactorEmail.Text = this.supplier.ContactorEmail;
            this.txtContactorMobile.Text = this.supplier.ContactorMobile;
            this.txtContactorPhone.Text = this.supplier.ContactorPhone;
        }
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
            this.txtCode.Properties.MaxLength = 8;
            this.txtMobile.Properties.MaxLength = 16;
            this.txtPhone.Properties.MaxLength = 16;
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
           
            this.txtCode.Text = String.Empty;
            this.txtName.Text = String.Empty;
            this.txtLiabilities.Text = String.Empty;
            this.txtMobile.Text = String.Empty;
            this.txtPhone.Text = String.Empty;
            this.txtAddress.Text = String.Empty;
            this.txtEmail.Text = String.Empty;
            this.txtFax.Text=string.Empty;
            this.txtTaxCode.Text = string.Empty;
            this.txtWebsite.Text = string.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtContactor.Text = string.Empty;
            this.txtContactorEmail.Text = string.Empty;
            this.txtContactorMobile.Text = string.Empty;
            this.txtContactorPhone.Text = string.Empty;
            this.txtName.Focus();
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", ErrorType.User1);
            }
        }

        private void txtLiabilities_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtLiabilities.Text.Trim() == String.Empty)
            {
                this.dxepLiabilities.ClearErrors();
            }
        }

        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.supplierManager.HasCodeDuplicated(this.txtName.Text.Trim(), this.supplier.Id))
                {
                    this.dxepCode.SetError(this.txtCode, "Mã khách hàng " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã khách hàng khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtCode.Focus();
                    return;
                }

                this.dxepCode.ClearErrors();
            }
        }
        private void txtCode_Leave(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                this.dxepCode.SetError(this.txtCode, "Mã khách hàng không được để trống.", ErrorType.User1);
            }
        }


        private void txtMobile_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtMobile.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.supplierManager.HasMobileDuplicated(this.txtMobile.Text.Trim(), this.supplier.Id))
                {
                    this.dxepMobile.SetError(this.txtMobile, "Số điện thoại " + this.txtMobile.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtMobile.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }

        private void txtEmail_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtEmail.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.supplierManager.HasEmailDuplicated(this.txtEmail.Text.Trim(), this.supplier.Id))
                {
                    this.dxepEmail.SetError(this.txtEmail, "Email " + this.txtEmail.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtEmail.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }


        #endregion

        #region button_click

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //id = this.supplier.Id;


            //validation name
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            //validation Code
            else if (this.txtCode.Text.Trim().Length == 0)
            {
                this.dxepCode.SetError(this.txtCode, "Mã khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtCode.Focus();
                return;
            }
            else if (this.supplierManager.HasCodeDuplicated(this.txtCode.Text.Trim(),supplier.Id) == true)
            {
                this.dxepName.SetError(this.txtCode, "Mã khách hàng " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã khách hàng khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            //end validation Code
            else if (this.txtLiabilities.Text.Trim() == String.Empty)
            {
                this.dxepLiabilities.SetError(this.txtLiabilities, "Nhập công nợ ban đầu");
                this.txtLiabilities.Focus();
                return;
            }
            //Validation CreatedBy
            else if (this.txtModifiedBy.Text.Trim().Length == 0)
            {
                this.dxepModifiedBy.SetError(this.txtModifiedBy, "Lựa chọn tên nhân viên sửa đổi");
                this.txtModifiedBy.Focus();
                return;
            }
            else
            {
                this.dxepLiabilities.ClearErrors();
                this.dxepMobile.ClearErrors();
                this.dxepName.ClearErrors();
                this.dxepEmail.ClearErrors();
                this.dxepCode.ClearErrors();
                this.dxepModifiedBy.ClearErrors();
            }
            //End validation CreatedBy

            // End of validation
            /*****************************************************************/
            // Save into database
            var _supplier = new Supplier();
            _supplier.Id = this.supplier.Id;
            _supplier.Code = this.txtCode.Text.Trim();
            _supplier.Liabilities = float.Parse(this.txtLiabilities.Text.Trim());
            _supplier.Name = this.txtName.Text.Trim();
            _supplier.Mobile = this.txtMobile.Text.Trim();
            _supplier.Phone = this.txtPhone.Text.Trim();
            _supplier.Address = this.txtAddress.Text.Trim();
            _supplier.Email = this.txtEmail.Text.Trim();
            _supplier.Fax = this.txtFax.Text.Trim();
            _supplier.Website = this.txtWebsite.Text.Trim();
            _supplier.TaxCode = this.txtTaxCode.Text.Trim();
            _supplier.Description = this.txtDescription.Text.Trim();

            _supplier.Status = 1;

            _supplier.CreatedBy = this.supplier.CreatedBy;
            _supplier.CreatedDate = DateTime.Now;
            _supplier.ModifiedBy = this.txtModifiedBy.Text;
            _supplier.ModifiedDate = this.dteModifiedDate.DateTime;
            _supplier.Contactor = this.txtContactor.Text.Trim();
            _supplier.ContactorEmail = this.txtContactorEmail.Text.Trim();
            _supplier.ContactorMobile = this.txtMobile.Text.Trim();
            _supplier.ContactorPhone = this.txtPhone.Text.Trim();
            this.supplierManager.UpdateSupplier(_supplier);
            XtraMessageBox.Show("Sửa dữ liệu thành công");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion


        

   
    }
}
